const executeBuild = require("./executeBuild");
const parseUI = require("./parseUI");
const startLocalService = require("./startLocalService");
const config = require("../mfo.config.js");
const fs = require("fs");
const t = require("../config/dict");
const configSource = require("./configSource");
const { log, error } = require("./utils");
const existsIgnore = () => {
  return new Promise((resolve, reject) => {
    let ignore = [];
    if (process.env.IGNORE) {
      const currentPath = process.cwd();
      const currentFile = currentPath + "\\" + process.env.IGNORE;
      fs.exists(currentFile, function (exists) {
        if (exists) {
          ignore = require(currentFile).ignore;
        } else {
          ignore = require("../" + config.LOCALIGNORE).ignore;
        }
        resolve(ignore);
      });
    } else {
      ignore = require("../" + config.LOCALIGNORE).ignore;
      resolve(ignore);
    }
  });
};

module.exports = (context) => {
  return Promise.resolve(context)
    .then((context) => {
      return new Promise((resolve, reject) => {
        existsIgnore()
          .then((ignore) => {
            Object.keys(context.metadata.projectTypeConfig).forEach((n) => {
              ignore.includes(n) &&
                (context.metadata.projectTypeConfig[n] = false);
            });
            resolve(context);
          })
          .catch((err) => {
            log("warn", {
              message: [
                t(context.lang, "使用原始设置"),
                t(context.lang, "成功"),
                t(context.lang, "。"),
              ].join(" "),
            });
            error(err);
            resolve(context);
          });
      });
    })
    .then((context) => {
      if (
        context.metadata.projectMode &&
        context.metadata.projectMode === "auto"
      ) {
        return context;
      }
      return configSource(context);
    })
    .then((context) => {
      return context.metadata.projectTypeConfig.dependencies
        ? executeBuild(context)
        : (log("warn", {
            message: [
              t(context.lang, "跳过"),
              "dependencies",
              t(context.lang, "。"),
            ].join(" "),
          }),
          context);
    })
    .then((context) => {
      if (
        context.metadata.projectMode &&
        context.metadata.projectMode === "auto"
      ) {
        return context;
      }
      return context.metadata.projectTypeConfig.ui
        ? parseUI(context)
        : (log("warn", {
            message: [
              t(context.lang, "跳过"),
              "ui",
              t(context.lang, "。"),
            ].join(" "),
          }),
          context);
    })
    .then((context) => {
      if (
        context.metadata.projectMode &&
        context.metadata.projectMode === "auto"
      ) {
        return context;
      }
      return context.metadata.projectTypeConfig.vaild
        ? startLocalService(context)
        : (log("warn", {
            message: [
              t(context.lang, "跳过"),
              "vaild",
              t(context.lang, "。"),
            ].join(" "),
          }),
          context);
    });
};
